//
//  SAPAudioTableViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/16.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPAudioTableViewCell.h"

@implementation SAPAudioTableViewCell
- (void)dealloc {
    [_collectionView release];
    [_oneModel release];
    
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self collectionViews];
        [SAPNetWorkTool getWithUrl:audio parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
            NSDictionary *dic = [result valueForKey:@"result"];
            self.oneModel = [[SAPAudioOneModel alloc] initWithDataSource:dic];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        } fail:^(NSError *error) {
            NSLog(@"%@", error);
        }];

    }return self;
}
- (void)collectionViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_SIZE.width / 3, SCREEN_SIZE.width / 3 + 20);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[SAPAudioCollectionViewCell class] forCellWithReuseIdentifier:@"SAPAudioCollectionViewCellIndentifier"];
    [self addSubview:self.collectionView];
    [_collectionView release];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height * 2);
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.oneModel.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SAPAudioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPAudioCollectionViewCellIndentifier" forIndexPath:indexPath];
    SAPAudioTwoModel *twoModel = self.oneModel.listArray[indexPath.item];
    cell.twoModel = twoModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
       
    SAPAudioTwoModel *twoModel = self.oneModel.listArray[indexPath.item];
    NSString *string = [NSString stringWithFormat:@"http://223.99.255.20/app.api.autohome.com.cn/autov5.1.0/News/newsonewradiolist-pm1-aid%@-p1-s50-tp0.json", twoModel.album_id];
    
    [self.delegate passName:twoModel.album_name withId:string withDescrition:twoModel.album_descrition withImgurl:twoModel.album_imgurl withBigimgurl:twoModel.album_bigImgurl];
    
    
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
